package cn.tedu.student;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.Date;
import java.util.Arrays;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@RestController
@RequestMapping("sctr")
public class StuController {
    @RequestMapping("eat")
    public void eat(Student s) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///mydemo1", "root", "root");
        String sql ="insert into student(name,age,sex,hobby,edu,intime) values (?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,s.getName());
        ps.setInt(2,s.getAge());
        ps.setString(3,s.getSex());
        //把hobby的值从[]变成字符串，存入数据库
        String hs = Arrays.toString(s.getHobby());
        ps.setString(4,hs.substring(1,hs.length()-1));
        ps.setString(5,s.getEdu());
//        java.sql.Date s1= new java.sql.Date(s.getIntime().getTime());
        ps.setDate(6,new Date(s.getIntime().getTime()));

        ps.executeUpdate();
        ps.close();
        System.out.println("数据入库成功~~");
        conn.close();
    }
}
